﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Reflection;
namespace XYZshell.Game.CardGameEngine
{
	public static  class GameContext
	{

		public static Game Game
		{
			get;
			set;
		}
		public static Player CurOperatePlayer
		{
			get;
			set;
		}

		public static object GetValue( object  obj, string name )
		{
			return obj.GetType().GetProperty( name ).GetValue( obj, null );
		}
		public static void  SetValue( object obj, string name, object value )
		{
			obj.GetType().GetProperty( name ).SetValue( obj, value, null );
		}

		public static void SetValue(string path,object value)
		{

			if (IsSpecialPath( path ))
			{
				switch (path)
				{
					case "#":
						( typeof( GameContext ) ).GetProperty( "CurOperatePlayer" ).SetValue( null, value,null );
						break;
				}
				return;
			}
			int edx=path.LastIndexOf( '.' );
			string obj=path.Substring( 0, edx );
			string m=path.Substring( edx + 1 );
			object o=GameContext.Find( obj );
			GameContext.SetValue( o, m, value );
		}
		public static void Invoke( object obj, string name, object[] parameters )
		{
			obj.GetType().GetMethod( name ).Invoke( obj, parameters );
		}

		public  static bool IsSpecialPath( string path )
		{
			if(path.Contains('#')) return true;
			return false;
		}
		/// <summary>
		/// 根据路径查找对象
		/// </summary>
		/// <param name="path">路径</param>
		/// <returns>查找的结果</returns>
		public static   object Find( string path )
		{
			switch (path)
			{
				case "#":
					return CurOperatePlayer;
				case "#-":
					return CurOperatePlayer.PreviousPlayer;
				case "#+":
					return CurOperatePlayer.NextPlayer;
			}
			string[] ps=path.Split( ".".ToArray(), StringSplitOptions.RemoveEmptyEntries );
			object    o=Game;
			foreach (string p in ps)
			{
				string mem=p;
				string idx=getPathIndexValue( ref  mem );
				if (idx != null)
				{
					IList r=(IList)GetValue(o, mem );

					MethodInfo minfo=null;


					int index=0;
					object[] args=null;
					if (int.TryParse( idx, out index ))
					{
						args = new object[] { index };
						minfo = r.GetType().GetMethod( "get_Item", new Type[] { index.GetType() } );
					}
					else
					{
						args = new object[] { idx };
						minfo = r.GetType().GetMethod( "get_Item", new Type[] { idx.GetType() } );
					}
					object i=  minfo.Invoke( r, args );
					o = i;

				}
				else
				{

					 o = GetValue(o, mem );

				}
				//TODO: 根据路径获得对象
			}

			return o;
		}



		static  string getPathIndexValue( ref string pathPart )
		{
			Regex rgx=new Regex( @"(?<n>\w+)\[(?<val>.+)\]" );
			if (rgx.IsMatch( pathPart ))
			{
				Match m=rgx.Match( pathPart );
				pathPart = m.Result( "${n}" );
				return m.Result( "${val}" ).Trim( "\"".ToArray() );
				;
			}

			return null;
		}
		///// <summary>
		///// 
		///// </summary>
		///// <param name="path"></param>
		///// <returns></returns>
		//public  object  FindOne( string path )
		//{
		//    return ((IList)Find( path ))[0];
		//}
	}
}
